<template>
  <a-row class="globalHeader" style="margin-bottom: 16px" align="center">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        @selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <!-- 不懂为啥这里要用一个selectedKeys-->
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/image.jpg" />
            <div class="title">Wren OJ</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in routes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div>{{ store.state.user?.loginUser?.userName ?? "未登录" }}</div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "../router/routes";
import { useRoute, useRouter } from "vue-router";
import { ref } from "vue";
import { useStore } from "vuex";

//使用userRouter来做跳转
const router = useRouter();

//将路由的信息同步到菜单栏上
//const route = useRoute();
//console.log(route);

//默认组件
const selectedKeys = ref(["/"]);

// 路由跳转时，更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

const store = useStore();
console.log(store.state.user.loginUser.userName);

//3秒钟之后
setTimeout(() => {
  store.dispatch("user/getLoginUser", {
    userName: "杨文龙",
    role: "admin",
  });
}, 10000);

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
</script>

<style scoped>
.title-bar {
  height: 48px;
  align-items: center;
  display: flex;
  width: auto;
}

.title {
  color: #444;
  font-size: 16px;
  margin-left: 16px;
}

.logo {
  width: 65px;
  height: auto;
  object-fit: contain;
}
</style>
